<template>
  <div>
    <el-dialog title="加入部门" :close-on-click-modal="false" :visible.sync="dialogVisible" width="716px" :lock-scroll="false" @close="cancel">
      <div class="org-box">
        <el-tree ref="department" class="tree-border m-t" :data="dataOrgTree" show-checkbox node-key="id" empty-text="加载中，请稍候" default-expand-all
          :highlight-current="true" :expand-on-click-node="false" :props="defaultProps" @check="currentChecked" />
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="save()">确认</el-button>
        <el-button @click="cancel()">取消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { joinDepartment } from '@/api/department.js'
// import { departmentTree } from '@/api/projectManage'
export default {
  name: 'BasisOrgsJoinOrg',
  data() {
    return {
      dialogVisible: false,
      defaultProps: {
        children: 'childrenDept',
        label: 'deptName'
      },
      departmentIds: [],
      currentUser: [],
      dataOrgTree: [],
      currentCheck: null
    }
  },
  created() {
    this.initTree()
  },
  methods: {
    initTree() {
      // departmentTree().then(res => {
      //   this.dataOrgTree = res.data
      // }).catch(err => {
      //   console.log(err)
      // })
    },
    show(params) {
      this.initTree()
      this.$nextTick(() => {
        this.departmentIds.forEach((item) => {
          this.$refs.department.setChecked(item, true, false)
        })
      })
      if (Array.isArray(params)) {
        this.currentUser = params
      } else {
        this.currentUser.push(params)
      }
      this.dialogVisible = true
    },
    save() {
      if (this.departmentIds && this.departmentIds.length === 0) {
        this.$message.warning('请选择需要加入的部门！')
        return false
      }
      const params = {
        userIds: [],
        departmentIds: this.departmentIds
      }
      this.currentUser.map((item) => {
        params.userIds.push(item.userId)
      })
      joinDepartment(params).then(() => {
        this.cancel()
        this.$parent.initUser()
        this.$parent.initTree()
      })
    },
    cancel() {
      this.departmentIds = []
      this.currentUser = []
      this.dialogVisible = false
    },
    // 所有菜单节点数据
    getMenuAllCheckedKeys() {
      // const checkedKeys = this.$refs.department.getCheckedKeys()
      // const halfCheckedKeys = this.$refs.department.getHalfCheckedKeys()
      // checkedKeys.unshift.apply(checkedKeys, halfCheckedKeys)
      // this.departmentIds = checkedKeys
      const halfCheckedKeys = this.$refs.department.getHalfCheckedKeys()
      const currentCheckedKeys = [this.currentCheck.id]
      currentCheckedKeys.unshift.apply(currentCheckedKeys, halfCheckedKeys)
      this.departmentIds = currentCheckedKeys

      return currentCheckedKeys
    },
    currentChecked(val) {
      this.currentCheck = val
      this.getMenuAllCheckedKeys()
    }
  }
}
</script>

<style lang="less" scoped>
  ::v-deep.el-dialog__body {
    padding-top: 0px;
  }
</style>
